package com.whfc.mach.manager.warn.impl;

import com.alibaba.fastjson.JSON;
import com.whfc.base.enums.AppModuleType;
import com.whfc.base.param.AlarmMessageParam;
import com.whfc.common.enums.WorkState;
import com.whfc.common.util.DateUtil;
import com.whfc.mach.dao.AppMachWarnRecordMapper;
import com.whfc.mach.dto.warn.MachWarnCheckDTO;
import com.whfc.mach.dto.warn.MachWarnRuleDTO;
import com.whfc.mach.entity.AppMachWarnRecord;
import com.whfc.mach.enums.AppMachWarnRuleType;
import com.whfc.mach.manager.warn.AbstractWarnManager;
import com.whfc.mach.manager.warn.AppWarnManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 设备硬件报警规则
 *
 * @author guodong_sun
 */
@Service(value = "machDeviceWarnCheckManagerImpl")
public class MachDeviceWarnCheckManagerImpl extends AbstractWarnManager implements AppWarnManager {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AppMachWarnRecordMapper appMachWarnRecordMapper;

    private static String DEVICE_WARN_CONTENT = "%s %s 在 %s,硬件被拆卸,请您及时处理";

    @Override
    public boolean checkValue(MachWarnCheckDTO machWarnCheckDTO, MachWarnRuleDTO machWarnRuleDTO) {
        logger.info("检测是否触发设备的硬件拆卸报警,machWarnCheckDTO:{},machWarnRuleDTO:{}", machWarnCheckDTO.toString(), machWarnRuleDTO.toString());
        Integer workState = machWarnCheckDTO.getWorkState();
        return WorkState.POWER.getValue().equals(workState);
    }

    @Override
    public AlarmMessageParam generateWarnRecord(MachWarnCheckDTO machWarnCheckDTO, MachWarnRuleDTO machWarnRuleDTO) {
        String triggerParam = JSON.toJSONString(machWarnCheckDTO);
        // 生成报警记录
        String objectId = String.valueOf(machWarnCheckDTO.getMachId());
        AppMachWarnRecord record = new AppMachWarnRecord();
        Date time = machWarnCheckDTO.getTriggerTime();
        record.setDeptId(machWarnCheckDTO.getDeptId());
        record.setRuleType(machWarnRuleDTO.getRuleType());
        record.setRuleId(machWarnRuleDTO.getRuleId());
        record.setTriggerObjectId(objectId);
        record.setTriggerTime(time);
        record.setRuleExpress(machWarnRuleDTO.getRuleExpress());
        record.setRuleParam(machWarnRuleDTO.getRuleParam());
        record.setTriggerParam(triggerParam);
        appMachWarnRecordMapper.insertSelective(record);
        logger.info("生成硬件拆卸报警,record:{}", record);

        // 组装生成消息参数
        AlarmMessageParam alarmMessageParam = new AlarmMessageParam();
        String content = String.format(DEVICE_WARN_CONTENT, machWarnCheckDTO.getMachTypeName(), machWarnCheckDTO.getMachCode(), DateUtil.formatDateTime(time));
        alarmMessageParam.setAlarmId(String.valueOf(record.getId()));
        alarmMessageParam.setContent(content);
        alarmMessageParam.setModuleType(AppModuleType.MACH.value());
        alarmMessageParam.setDeptId(record.getDeptId());
        alarmMessageParam.setRuleId(record.getRuleId());
        alarmMessageParam.setTime(time);
        alarmMessageParam.setTitle(AppMachWarnRuleType.DEVICE.tittle());
        alarmMessageParam.setTriggerObjectId(objectId);
        alarmMessageParam.setRuleType(machWarnRuleDTO.getRuleType());
        return alarmMessageParam;
    }


}
